/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 17:53:51
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;
using LEARNREN.COM.DAL;
using System.Web;

namespace LEARNREN.COM.BLL
{
    public class CatalogueLogic : LogicBase<Catalogue>
    {
        public CatalogueLogic()
        {

        }
        public CatalogueLogic(DB4ODALClient client)
            : base(client)
        {

        }

        public Catalogue GetById(long Id)
        {
            return GetList(p => p.Id == Id).FirstOrDefault();
        }

        /// <summary>
        /// 从缓存中获取指定Id的子栏目集合
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public IEnumerable<Catalogue> GetChildren(long parentId)
        {
            return BasicCache.AllCatalogueList.Where(p => p.ParentId == parentId).OrderBy(p => p.Order);
        }

        /// <summary>
        /// 数据从缓存中获取获取配置文件中指定Id的栏目的所有子栏目
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Catalogue> GetConfigedProductCatalogueCollection()
        {
            string spid = UtilityTool.GetConfiguration("leftnavpid");
            long pid = 0;
            if (long.TryParse(spid, out pid))
            {
                return BasicCache.AllCatalogueList.Where(p => p.ParentId == pid).OrderBy(p => p.Order);
            }
            else
            {
                return new List<Catalogue>();
            }
        }

        /// <summary>
        /// 从缓存中按id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Catalogue GetByIdFromCache(long id)
        {
            return BasicCache.AllCatalogueList.FirstOrDefault(p => p.Id == id);
        }

        /// <summary>
        /// 增加栏目内容数
        /// </summary>
        /// <param name="id">栏目id</param>
        public void IncreaseContentCount(long id)
        {
            Catalogue c = GetById(id);
            c.ContentCount += 1;
            Save(c);

            // 更新缓存
            BasicCache.AllCatalogueList.RemoveAll(p => p.Id == id);
            BasicCache.AllCatalogueList.Add(c);
        }

        /// <summary>
        /// 减少栏目内容数
        /// </summary>
        /// <param name="id"></param>
        public void DecreaseContentCount(long id)
        {
            Catalogue c = GetById(id);
            c.ContentCount -= 1;
            Save(c);

            // 更新缓存
            BasicCache.AllCatalogueList.RemoveAll(p => p.Id == id);
            BasicCache.AllCatalogueList.Add(c);
        }

        /// <summary>
        /// 获取用于区分上下级的路径
        /// </summary>
        /// <param name="catalogue"></param>
        /// <returns></returns>
        public string GetPath(Catalogue catalogue)
        {
            if (catalogue.ParentId == 0)
            {
                return string.Format("{0},{1}", "^", catalogue.Id);
            }
            else
            {
                Catalogue parent = GetByIdFromCache(catalogue.ParentId);
                if (parent == null)
                {
                    parent = GetById(catalogue.ParentId);
                }
                if (parent != null)
                {
                    return string.Format("{0},{1}", parent.Path, catalogue.Id);
                }
                else
                {
                    return string.Format("{0},{1}", "^", catalogue.Id);
                }
            }
        }

        /// <summary>
        /// 获取指定栏目的URL地址
        /// 如果网站设置为静态，则返回静态格式的地址。相反返回动态格式的地址。
        /// </summary>
        /// <param name="cid">栏目id</param>
        /// <param name="page">页码</param>
        /// <returns></returns>
        public string GetCatalogueURL(long cid, int page)
        {
            Catalogue c = GetByIdFromCache(cid);
            return GetCatalogueURL(c, page);
        }

        /// <summary>
        /// 获取指定栏目的URL地址
        /// 如果网站设置为静态，则返回静态格式的地址。相反返回动态格式的地址。
        /// </summary>
        /// <param name="c">栏目实体</param>
        /// <param name="page">页码</param>
        /// <returns></returns>
        public string GetCatalogueURL(Catalogue c, int page)
        {
            if (BasicCache.SiteConfig.IsStaticEnable)
            {
                return GetStaticURL(c, page);
            }
            else
            {
                return GetDynamicURL(c, page);
            }
        }

        /// <summary>
        /// 如果网站设置成静态则声称静态形式的地址
        /// </summary>
        /// <param name="c"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        private string GetStaticURL(Catalogue c, int page)
        {
            return string.Format("{0}{1}/{2}/index{3}.html",
                                 BasicCache.SiteConfig.SiteUrl,
                                 VirtualPathUtility.ToAbsolute("~/"),
                                 c.HtmlPath,
                                 page == 0 ? string.Empty : "_" + page);
        }

        /// <summary>
        /// 如果网站设置成动态则声称动态形式的地址
        /// </summary>
        /// <param name="c"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        private string GetDynamicURL(Catalogue c, int page)
        {
            return VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}&page={2}", c.CatalogueType, c.Id, page));
        }
    }
}
